/**
 * 
 */
package cardbs.dao;

import cardbs.domain.Service;
import static cardbs.servlets.CreateBillServlet.*;
import cardbs.domain.Order;
import cardbs.domain.Vehicle;
import cardbs.domain.Status;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/** 
 * <!-- begin-UML-doc -->
 * <!-- end-UML-doc -->
 * @author marianna
 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
 */
public class ServiceDAO extends GenericDAO<Service> {
	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @param vehicle
	 * @param status
	 * @return
	 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public Service findService(Vehicle vehicle, Status status) {
		// begin-user-code
		// TODO Auto-generated method stub
			String trafficNo = (String)vehicle.getTrafficNo();
			EntityManager em = JPAUtil.getCurrentEntityManager();
			Query query = em.createQuery("select service from Service service where trafficNo=:insertedTrafficNo and status=:insertedStatus");
		    query.setParameter("insertedStatus", status);
		    query.setParameter("insertedTrafficNo", trafficNo);
		    List <Service> results = query.getResultList();
		    
			int size = results.size();
		    if (size>0){
		    	return results.get(0);
		    }
			return null;
			// end-user-code
		// end-user-code
	}
	public Set<Service> findServicesByVehicle(Vehicle vehicle){
		// begin-user-code
		// TODO Auto-generated method stub
		String trafficNo = (String)vehicle.getTrafficNo();
		EntityManager em = JPAUtil.getCurrentEntityManager();
		Query query = em.createQuery("select service from Service service where trafficNo=:insertedTrafficNo");
		query.setParameter("insertedTrafficNo", trafficNo);
	    List <Service> results = query.getResultList();
	    Set<Service> services = new HashSet(results);
		int size = results.size();
	   
	    if (size!=0){
	    	return services;
	    }
		return null;
		// end-user-code
	}
	
	public Set<Service> findAllServices() {
		// begin-user-code
		// TODO Auto-generated method stub
	     CriteriaBuilder builder = JPAUtil.getEntityManagerFactory().getCriteriaBuilder();
	     CriteriaQuery<Service> criteria = builder.createQuery( Service.class );
	     Root<Service> service = criteria.from( Service.class );
	     criteria.select(service);
	     List<Service> allservices = JPAUtil.getCurrentEntityManager().createQuery(criteria).getResultList();
	     Set<Service> services = new HashSet(allservices);
	     return services;
}
	
	public Set<Service> findServicesByStatus(Status status){
		// begin-user-code
		// TODO Auto-generated method stub
		
		EntityManager em = JPAUtil.getCurrentEntityManager();
		Query query = em.createQuery("select service from Service service where status=:insertedStatus");
	    query.setParameter("insertedStatus", status);
	    List <Service> results = query.getResultList();
	    Set<Service> services = new HashSet(results);
		int size = results.size();
	  
	    if (size!=0){
	    	return services;
	    }
		return null;
		// end-user-code
	}
	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @param newService
	 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void save(Service newService) {
		super.save(newService);
		// begin-user-code
		// TODO Auto-generated method stub

		// end-user-code
	}

	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @param headmaster
	 * @param period
	 * @return
	 * @throws ParseException 
	 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public Set<Service> findServicesPeriodHeadmaster(int headmasterCode, String startPeriod, String endPeriod) throws ParseException {
		// begin-user-code
		// TODO Auto-generated method stub
		DateFormat formatter ; 
		Date startDate ; 
		Date endDate ; 
		formatter = new SimpleDateFormat("dd-MM-yy");
		startDate = (Date)formatter.parse(startPeriod); 
		endDate = (Date)formatter.parse(endPeriod); 
		Calendar startCal=Calendar.getInstance();
		startCal.setTime(startDate);
		Calendar endCal=Calendar.getInstance();
		endCal.setTime(endDate);
				
		Status statusC = Status.valueOf(Status.class, "completed");
		
		EntityManager em = JPAUtil.getCurrentEntityManager();
		Query query = em.createQuery("select service from Service service where status=:insertedStatus and headmasterCode=:insertedHeadmasterCode and startDate>=:insertedStartDate and endDate<=:insertedEndDate");
	    query.setParameter("insertedHeadmasterCode", headmasterCode);
	    query.setParameter("insertedStartDate", startCal);
	    query.setParameter("insertedEndDate", endCal);
	    query.setParameter("insertedStatus", statusC);
	    List <Service> results = query.getResultList();
	    Set<Service> services = new HashSet(results);
		int size = results.size();
	    em.close();
	    if (size!=0){
	    	return services;
	    }
		return null;
		// end-user-code
	}


	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @param service
	 * @param part
	 * @return
	 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public Integer findPartAmount(Object service, Object part) {
		// begin-user-code
		// TODO Auto-generated method stub
		return null;
		// end-user-code
	}
}